package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.TalkService;
import com.zfei.gfb.api.domain.StatsTag;
import com.zfei.gfb.api.domain.StatsValue;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbGreenPlotApplyMapper;
import com.zfei.gfb.dao.GfbGreenPlotMapper;
import com.zfei.gfb.device.AppContext;
import com.zfei.gfb.model.GfbGreenPlot;
import com.zfei.gfb.model.GfbUser;
import com.zfei.gfb.query.GfbGreenPlotQuery;
import com.zfei.gfb.request.*;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.GfbGreenPlotListVo;
import com.zfei.gfb.vo.GfbGreenPlotVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @version 1.0
 * @author
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "菜地列表接口")
@RestController
@RequestMapping("/web/plot")
public class GfbGreenPlotController  {

	@Resource
	private GfbGreenPlotMapper gfbGreenPlotMapper;

	@Resource
	private GfbGreenPlotApplyMapper gfbGreenPlotApplyMapper;

	@Resource
	private HttpSession httpSession;

	@Resource
	private TalkService talkService;

	@Resource
	private StatsService statsService;

	//todo 浏览量
	@ApiOperation(value = "列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbGreenPlotListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list(@RequestBody GfbGreenPlotQuery query) {
		query.build(Now::millis);

		GfbGreenPlotListVo result = new GfbGreenPlotListVo();

		int total = gfbGreenPlotMapper.total(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbGreenPlot> list = gfbGreenPlotMapper.list(query);
		List<GfbGreenPlotVo> gfbGreenPlotVos  = Beans.copys(list,GfbGreenPlotVo.class);
		result.setData(gfbGreenPlotVos);

		for (GfbGreenPlotVo gfbGreenPlotVo : gfbGreenPlotVos) {
			gfbGreenPlotVo.setBrowseNum(statsService.get(gfbGreenPlotVo.getId().toString(), StatsTag.GREEN_BROWSE_COUNT).getNum());
		}
		return result;

	}

	@ApiOperation(value = "新增")
	@LogResult
	@HttpJson("/add")
	public Object add(@RequestBody GfbGreenPlotAddRequest request) {

		request.checkInsertNull();
		GfbGreenPlot record = Beans.copy(request, GfbGreenPlot.class);

		GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);

		record.setStatus(Const.STATUS_OFF);
		record.setUserId(user.getId());
		record.setImages(Gsons.toJson(request.getImages()));
		record.setPubTime(Now.millis());

		record.setCreateTime(Now.millis());
		record.setUpdateTime(Now.millis());
		record.setCntime(DateUtil.now());

		gfbGreenPlotMapper.insertSelective(record);
		return null;
	}


	@ApiOperation(value = "编辑")
	@LogResult
	@HttpJson("/edit")
	public Object edit(@RequestBody GfbGreenPlotEditRequest request) {
		request.checkUpdateNull();

		GfbGreenPlot record = gfbGreenPlotMapper.selectByPrimaryKey(request.getId());
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}
		GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);
		BeanUtils.copyProperties(request, record);

		record.setUserId(user.getId());
		record.setImages(Gsons.toJson(request.getImages()));
		record.setPubTime(Now.millis());

		record.setUpdateTime(Now.millis());
		gfbGreenPlotMapper.updateByPrimaryKeySelective(record);

		return null;
	}

	@ApiOperation(value = "删除")
	@LogResult
	@HttpJson("/del")
	public Object del(@RequestBody GfbDelRequest req) {
		Integer id = req.getId();

		if (id == null) {
			throw new StatusException(Status.INVALID_ARGUMENT);
		}

		GfbGreenPlot record = gfbGreenPlotMapper.selectByPrimaryKey(id);
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		gfbGreenPlotMapper.deleteByPrimaryKey(id);

		gfbGreenPlotApplyMapper.deleteByPlotId(id);

		return null;
	}

	@ApiOperation(value = "一键认领/取消认领")
	@LogResult
	@HttpJson("/set")
	public Object set(@RequestBody GfbSetRequest req) {
		req.checkSetNull(Const::legalTwoStatus);
		Integer status = req.getStatus();
		Integer id = req.getId();

		GfbGreenPlot record = gfbGreenPlotMapper.selectByPrimaryKey(id);
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);
		record.setUserId(user.getId());
		record.setStatus(status);
		gfbGreenPlotMapper.updateByPrimaryKeySelective(record);

		return null;
	}

	@ApiOperation(value = "菜地详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = GfbGreenPlotVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbGreenPlot record = gfbGreenPlotMapper.selectByPrimaryKey(id);

		GfbGreenPlotVo vo = Beans.copy(record, GfbGreenPlotVo.class);
		List<String> images = Gsons.fromJson(record.getImages(), Types.StringListType);
		vo.setImages(images);

		return vo;
	}
}
